﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMS.Model;
using CMS.Contracts;
using System.ServiceModel;

namespace Presentation
{
    public partial class Create : System.Web.UI.Page
    {
        private IProjectBService channelObject;
        private ChannelFactory<IProjectBService> ChannelFactory = new ChannelFactory<IProjectBService>("CMSClient");

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                channelObject = ChannelFactory.CreateChannel();

                List<Department> depList = new List<Department>();
                using (channelObject as IDisposable)
                {
                    depList = channelObject.GetAllDepartments();
                }
                

                DropDownListDepartment.DataSource = depList;
                //DropDownListDepartment.DataValueField = "DepartmentId";
                //DropDownListDepartment.DataTextField = "DepartmentName";
                //DropDownListDepartment.DataBind();
                DropDownListDepartment.Items.Clear();
                DropDownListDepartment.Items.Capacity = depList.Count;
                DropDownListDepartment.Items.Insert(0, "Please Select");
                foreach (var a in depList)
                {
                    ListItem b = new ListItem(a.DepartmentName);
                    if (!DropDownListDepartment.Items.Contains(b))
                    {
                        DropDownListDepartment.Items.Insert(a.DepartmentId, a.DepartmentName);

                    }

                }
            }
        }

        protected void Button_Create_Click(object sender, EventArgs e) //return teacher list page
        {
            string Gender = null;

            if (0 == RadioButtonList1.SelectedIndex)
            {
                Gender = "Male";
            }
            else
            {
                Gender = "Female";
            }


            int num = DropDownListDepartment.SelectedIndex;
            List<Department> depList = new List<Department>();

            channelObject = ChannelFactory.CreateChannel();
            using (channelObject as IDisposable)
            {
                depList = channelObject.GetDepartmentById(num);

                Department mydep = new Department();
                foreach (var p in depList)
                {
                    mydep = p;
                }

                if ("" == TeacherNameBox.Text|| 0 == num)
                {
                    Response.Write("<script>alert('Name or department is not available,please retry');window.location.href='CreateTeacher.aspx';</script>");
                }
                else
                {
                    Teacher par = new Teacher(0, TeacherNameBox.Text, Gender, num, mydep);

                    try
                    {
                        channelObject.AddTeacher(par);
                        Response.Redirect("TeacherList.aspx");
                    }

                    catch (Exception ex)
                    {
                        Response.Write("<script>alert(ex.Message);window.location.href='TeacherList.aspx';</script>");
                    }
                }

                Response.End();//Send buffer to client ,then stop executing

            }

        }
    }
    
}